﻿///////////////////////////////////////////////////////////////////////////////
// File:        Log.h
// Author:      671643387@qq.com
// Date:        2016年1月18日 上午11:34:20
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef SHARED_LOG_H_
#define SHARED_LOG_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>

enum eLogSeverityLevel
{
    kDebug,
    kInfo,
    kError,
    kCritical
};

template< typename CharT, typename TraitsT >  
inline std::basic_ostream< CharT, TraitsT >& operator<< (  
    std::basic_ostream< CharT, TraitsT >& strm, eLogSeverityLevel lvl)  
{  
    static const char* const str[] =  
    {  
        "debug",  
        "info",
        "error",  
        "critical"  
    };  
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))  
        strm << str[lvl];  
    else  
        strm << static_cast< int >(lvl);  
    return strm;  
}

class Log
{
private:
    const static int kMaxLogLength = 1024 * 128;

public:
    Log(void);
    ~Log(void);

    static void New(void);
    static void Free(void);

public:
    ::boost::log::sources::severity_logger<eLogSeverityLevel> &GetLogger(void)
    {
        return *src_;
    }

    void Debug(const char *format, ...);
    void Info(const char *format, ...);
    void Error(const char *format, ...);
    void Critical(const char *format, ...);

private:
    ::boost::shared_ptr< ::boost::log::sources::severity_logger<eLogSeverityLevel> > src_;
};

typedef ::boost::shared_ptr< Log >  LogPtr;

extern LogPtr gLog;

#define MWOW_DEBUG      BOOST_LOG_SEV(gLog->GetLogger(), kDebug)
#define MWOW_INFO       BOOST_LOG_SEV(gLog->GetLogger(), kInfo)
#define MWOW_ERROR      BOOST_LOG_SEV(gLog->GetLogger(), kError)
#define MWOW_CRITICAL   BOOST_LOG_SEV(gLog->GetLogger(), kCritical)

#define MWOW_DEBUG_F    gLog->Debug
#define MWOW_INFO_F     gLog->Info
#define MWOW_ERROR_F    gLog->Error
#define MWOW_CRITICAL_F gLog->Critical

#endif /* SHARED_LOG_H_ */
